import { Vec3 } from 'cc';
import { Vec3Util } from 'db://app-game/core/utils/Vec3Util';
import { ecs } from 'db://app-game/libs/ecs/ECS';
import { Role } from '../Role';
import { RoleModelComp } from '../model/RoleModelComp';

/** 角色移动组件 */
@ecs.register('RoleModelMove')
export class RoleModelMoveComp extends ecs.Comp {
  /** 移动速度 */
  velocity: Vec3 = new Vec3(0, 0);
  /** 角度 */
  angle: Vec3 = new Vec3(1, 0);
  reset(entity: ecs.Entity): void {
    this.velocity = new Vec3(0, 0);
    this.angle = new Vec3(1, 0);
  }
}

/** 角色移动系统 */
@ecs.register('RoleMove')
export class RoleMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpdate {
  filter(): ecs.IMatcher {
    return ecs.allOf(RoleModelComp, RoleModelMoveComp);
  }

  update(role: Role, dt: number): void {
    // 角色移动
    this.roleMove(role);
  }

  /** 角色移动 */
  roleMove(role: Role) {
    let velocity = role.RoleModelMove.velocity;
    if (velocity.length() <= 0) return;
    velocity = role.resetVelocity(velocity);
    role.RoleView.node.translate(velocity);
    let angle = Vec3Util.getAngle(role.RoleModelMove.angle);
    role.RoleView.node.getChildByName('body').setRotationFromEuler(0, 0, angle - 90);
  }
}
